<?php
# 文件名称:select.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_HTMLSelect
{
	function option( $value, $text='', $value_name='value', $text_name='text', $disable=false )
	{
		$obj = new stdClass;
		$obj->$value_name	= $value;
		$obj->$text_name	= trim( $text ) ? $text : $value;
		$obj->disable		= $disable;
		return $obj;
	}
	function toObject( $key, $value='', $key_name='key', $value_name='value')
	{
		$obj = new stdClass;
		$obj->$key_name	= trim( $key );
		$obj->$value_name	= trim( $value );		
		return $obj;
	}
	function toObjects( $arr, $key_name='key', $value_name='value')
	{
		$objs = array();
		foreach ($arr as $k => $v)
		{
			$obj = new stdClass;
			$obj->$key_name	= trim( $k );
			$obj->$value_name	= trim( $v );			
			$objs[] = $obj;
		}
		return $objs;
	}
	function optgroup( $text, $value_name = 'value', $text_name = 'text' )
	{
		$obj = new stdClass;
		$obj->$value_name	= '<OPTGROUP>';
		$obj->$text_name	= $text;
		return $obj;
	}
	/**
	 * Generates just the option tags for an HTML select list
	 *
	 */
	function options( $arr, $key = 'value', $text = 'text', $selected = null, $translate = false )
	{
		$html = '';
		foreach ($arr as $i => $option)
		{
			$element =& $arr[$i]; // since current doesn't return a reference, need to do this
			$isArray = is_array( $element );
			$extra	 = '';
			if ($isArray)
			{
				$k 		= $element[$key];
				$t	 	= $element[$text];
				$id 	= ( isset( $element['id'] ) ? $element['id'] : null );
				if(isset($element['disable']) && $element['disable']) {
					$extra .= ' disabled="disabled"';
				}
			}
			else
			{
				$k 		= $element->$key;
				$t	 	= $element->$text;
				$id 	= ( isset( $element->id ) ? $element->id : null );
				if(isset( $element->disable ) && $element->disable) {
					$extra .= ' disabled="disabled"';
				}
			}
			// This is real dirty, open to suggestions,
			// barring doing a propper object to handle it
			if ($k === '<OPTGROUP>') {
				$html .= '<optgroup label="' . $t . '">';
			} else if ($k === '</OPTGROUP>') {
				$html .= '</optgroup>';
			}
			else
			{
				//if no string after hypen - take hypen out
				$splitText = explode( ' - ', $t, 2 );
				$t = $splitText[0];
				if(isset($splitText[1])){ $t .= ' - '. $splitText[1]; }
				//$extra = '';
				//$extra .= $id ? ' id="' . $arr[$i]->id . '"' : '';
				if (is_array( $selected ))
				{
					foreach ($selected as $val)
					{
						$k2 = is_object( $val ) ? $val->$key : $val;
						if ($k == $k2)
						{
							$extra .= ' selected="selected"';
							break;
						}
					}
				} else {
					$extra .= ( (string)$k == (string)$selected  ? ' selected="selected"' : '' );
				}
				//if flag translate text
				if ($translate) {
					$t = C_text::_( $t );
				}
				if(strlen($t)>20)
				{
					$SYS=&get_instance();
					$SYS->loader->helper('string');
					$t=cutstr($t,20);
				}
				$html .= '<option value="'. $k .'" '. $extra .'>' . $t . '</option>';
			}
		}
		return $html;
	}

	/**
	 * Generates an HTML select list
	 *
	 */
	function genericlist( $arr, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false )
	{
		if ( is_array( $arr ) ) {
			reset( $arr );
		}
		$id = $name;
		if ( $idtag ) {
			$id = $idtag;
		}
		$id		= str_replace('[','',$id);
		$id		= str_replace(']','',$id);
		$html	= '<select name="'. $name .'" id="'. $id .'" '. $attribs .'>';
		$html	.= C_HTMLSelect::Options( $arr, $key, $text, $selected, $translate );
		$html	.= '</select>';
		return $html;
	}
	/**
	* Generates a select list of integers
	*
	*/
	function integerlist( $start, $end, $inc, $name, $attribs = null, $selected = null, $format = "" )
	{
		$start 	= intval( $start );
		$end 	= intval( $end );
		$inc 	= intval( $inc );
		$arr 	= array();
		for ($i=$start; $i <= $end; $i+=$inc)
		{
			$fi = $format ? sprintf( "$format", $i ) : "$i";
			$arr[] = C_html::_('select.option',  $fi, $fi );
		}
		return C_html::_('select.genericlist',   $arr, $name, $attribs, 'value', 'text', $selected );
	}
	/**
	* Generates an HTML radio list
	*
	*/
	function radiolist( $arr, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false )
	{
		reset( $arr );
		$html = '';
		$id_text = $name;
		if ( $idtag ) {
			$id_text = $idtag;
		}
		for ($i=0, $n=count( $arr ); $i < $n; $i++ )
		{
			$k	= $arr[$i]->$key;
			$t	= $translate ? C_text::_( $arr[$i]->$text ) : $arr[$i]->$text;
			$id	= ( isset($arr[$i]->id) ? @$arr[$i]->id : null);
			$extra	= '';
			$extra	.= $id ? " id=\"" . $arr[$i]->id . "\"" : '';
			if (is_array( $selected ))
			{
				foreach ($selected as $val)
				{
					$k2 = is_object( $val ) ? $val->$key : $val;
					if ($k == $k2)
					{
						$extra .= " selected=\"selected\"";
						break;
					}
				}
			} else {
				$extra .= ((string)$k == (string)$selected ? " checked=\"checked\"" : '');
			}
			$html .= "\n\t<input type=\"radio\" name=\"$name\" id=\"$id_text$k\" class='cb' value=\"".$k."\"$extra $attribs />";
			$html .= "\n\t<label for=\"$id_text$k\">$t</label>";
		}
		$html .= "\n";
		return $html;
	}
	/**
	* Generates a yes/no radio list
	*
	*/
	function booleanlist( $name, $attribs = null, $selected = null, $yes='yes', $no='no', $id=false )
	{
		$arr = array(
			C_html::_('select.option',  '0', C_text::_( $no ) ),
			C_html::_('select.option',  '1', C_text::_( $yes ) )
		);		
		return C_html::_('select.radiolist',  $arr, $name, $attribs, 'value', 'text', (int) $selected, $id );
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>
